﻿using AutoMapper;
using Comain.RBAC;
using Common;
using IService;
using IService.Rempos;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Reflection;
using System.Text;

namespace WdbApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class MemberController : ControllerBase
    {
        private readonly IMemberService memberss;

        public MemberController(IMemberService memberss)
        {
            this.memberss = memberss;
        }

        /// <summary>
		/// 会员增加
		/// </summary>
		/// <param name="t">会员实体</param>
		/// <returns>受影响的行数</returns>
        [HttpPost("AddMember")]
        public async Task<APIResult<int>> AddMember(T_Member t, string leList)
        {
            t.Member_Id = Guid.NewGuid();

            IList<int> ints1 = leList.Split(',').Select(i => int.Parse(i)).ToList();

            var memberinfo = await memberss.AddMember(t, ints1);

            return new APIResult<int>("")
            {
                Data = memberinfo,
                Message = "成功",
                Isucess = true,
                Code = ResultCode.OK
            };
        }


        /// <summary>
		/// 会员等级列表---显示
		/// </summary>
		/// <returns>返回会员等级列表</returns>
        [HttpGet("GetMemberList")]
        public async Task<APIResult<List<T_MemberLevel>>> GetMemberList()
        {
            var result = await memberss.GetMemberList();
            return new APIResult<List<T_MemberLevel>>("")
            {
                Data = result,
                Message = "成功",
                Isucess = true,
                Code = ResultCode.OK
            };
        }



















        /// <summary>
        /// 显示权限信息
        /// </summary>
        /// <returns></returns>
        //[Authorize]
        [HttpGet("ShowPerission")]
        public APIResult<List<TreeItem>> ShowPerission(int parentId)
        {
            var result = memberss.ShowPerission(parentId);
            return new APIResult<List<TreeItem>>("")
            {
                Data = result,
                Message = "成功",
                Isucess = true,
                Code = ResultCode.OK
            };
        }
        /// <summary>
        /// 生产JWT
        /// </summary>
        /// <returns></returns>
        [HttpGet("CreateTokenString")]
        public string CreateTokenString()
        {
            //私钥
            var secretByte = Encoding.UTF8.GetBytes("kdsfldsflkdslkflkdsflkdslfdslkflk");
            // 非对称加密
            var signingKey = new SymmetricSecurityKey(secretByte);
            // 使用256 生成数字签名
            var signingCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256);
            // 生成Token
            var token = new JwtSecurityToken(
                //声明发布者信息
                issuer: "BeiJingBW",
                //声明接受者信息
                audience: "BeiJingBW",
                //声明过期时间
                expires: DateTime.Now.AddDays(1), // 一天后过期
                //生成签名
                signingCredentials: signingCredentials
            );
            // 生成token 字符串
            var strToken = new JwtSecurityTokenHandler().WriteToken(token);
            return strToken;
        }
        /// <summary>
        /// 添加角色权限
        /// </summary>
        /// <param name="info">角色实体</param>
        /// <param name="perLists">权限编号</param>
        /// <returns>返回满足条件的数据</returns>
        [HttpPost("AddRolePermission")]
        public async Task<APIResult<int>> AddRolePermission(T_Role info, string perLists)
        {
            try
            {
                //将字符串类型的参数转换为接口
                IList<int> ints = perLists.Split(',').Select(x => int.Parse(x)).ToList();
                var res = await memberss.AddRolePermission(info, ints);
                return new APIResult<int>("")
                {
                    Data = res,
                    Code = ResultCode.OK,
                    Isucess = true,
                    Message = "添加角色权限成功"
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
        [HttpGet("Show")]
        public async Task<APIResult<PageComm<List<RolePerissionDTO>>>> Show(int index, int page, string? name)
        {
            var result = await memberss.Show(index, page, name);
            return new APIResult<PageComm<List<RolePerissionDTO>>>("")
            {
                Data = result,
                Code = ResultCode.OK,
                Isucess = true,
                Message = "显示角色权限成功"
            };
        }
    }
}
